package com.company.eos.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.eos.common.util.PageUtils;
import com.company.eos.mapper.AuditDetailMapper;
import com.company.eos.mapper.AuditExpenseMapper;
import com.company.eos.mapper.UserMapper;
import com.company.eos.domain.AuditDetail;
import com.company.eos.domain.AuditExpense;
import com.company.eos.domain.User;
import com.company.eos.service.AuditExpenseService;
import com.company.eos.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Real
 * @description 针对表【tb_audit_reim(报销单表)】的数据库操作Service实现
 * @createDate 2022-04-28 18:31:47
 */
@Service
public class AuditExpenseServiceImpl extends ServiceImpl<AuditExpenseMapper, AuditExpense>
        implements AuditExpenseService {

    @Autowired
    private AuditExpenseMapper auditExpenseMapper;
    @Resource
    private AuditDetailMapper auditDetailMapper;
    @Resource
    private UserMapper userMapper;
    @Autowired
    private UserService userService;

    /**
     * 查询报销分页数据
     *
     * @param param
     * @return
     */
    @Override
    public PageUtils searchExpenseByPage(HashMap param) {
        ArrayList<HashMap> list = auditExpenseMapper.searchExpenseByPage(param);
        long count = auditExpenseMapper.searchExpenseCount(param);
        int start = (Integer) param.get("start");
        int length = (Integer) param.get("length");
        PageUtils pageUtils = new PageUtils(list, count, start, length);
        return pageUtils;
    }

    /**
     * 新增报销记录
     *
     * @param expense
     * @return
     */
    @Override
    public int insert(AuditExpense expense) {
        return auditExpenseMapper.insert(expense);
    }

    /**
     * 根据ID查询报销单详情
     *
     * @param param
     * @return
     */
    @Override
    public HashMap searchExpenseById(HashMap param) {
        HashMap map = auditExpenseMapper.searchExpenseById(param);
        // 报销单ID
        String id = param.get("id").toString();
        // 添加复核人
        AuditExpense expense = auditExpenseMapper.selectById(id);
        Integer flowId = expense.getFlowId();
        AuditDetail detail = auditDetailMapper.selectOne(Wrappers.lambdaQuery(AuditDetail.class)
                .eq(AuditDetail::getFlowId, flowId));
        // 复核人ID
        Integer auditUserId = detail.getAuditUserId();
        User user = userMapper.selectById(auditUserId);
        map.put("auditUserName", user.getName());
        // 添加财务官
        List<User> users = userMapper.selectList(null);
        users.forEach(user1 -> {
            ArrayList<String> strings = userService.searchUserRoles(user1.getId());
            if (strings.contains("财务")) {
                map.put("finance", user1.getName());
            }
        });
        // 添加生成的二维码
        String instanceId = MapUtil.getStr(map, "instanceId");
        QrConfig qrConfig = new QrConfig();
        qrConfig.setWidth(70);
        qrConfig.setHeight(70);
        qrConfig.setMargin(2);
        String qrCodeBase64 = QrCodeUtil.generateAsBase64(instanceId, qrConfig, "jpg");
        map.put("qrCodeBase64", qrCodeBase64);
        return map;
    }

    /**
     * 删除报销记录
     *
     * @param param
     * @return
     */
    @Override
    public int deleteExpenseById(HashMap param) {
        return auditExpenseMapper.deleteExpenseById(param);
    }

}




